//
//  WJTagsCollectionView.m
//  WJBSApp
//
//  Created by dllo on 16/7/18.
//  Copyright © 2016年 王景伟. All rights reserved.
//

#import "WJTagsCollectionView.h"
#import "WJTagsCollectionCell.h"
#import "WJTagsModel.h"

@interface WJTagsCollectionView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic,retain) UICollectionView *tagsCollection;


@end

@implementation WJTagsCollectionView

static NSString *const TagCell = @"TagCell";

-(void)dealloc
{
    [_tagsCollection release];
    [_array release];
    Block_release(self.tagBlock);
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.tagsCollection.delegate = nil;
    self.tagsCollection.dataSource = nil;
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createView];
        
    }
    return self;
}

- (void)createView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    self.tagsCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH - 20, 15) collectionViewLayout:flowLayout];
    [self addSubview:self.tagsCollection];
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Mode"] isEqualToString:@"night"]) {
         self.tagsCollection.backgroundColor = FORBLACKCOLOR;
    }else{
         self.tagsCollection.backgroundColor = [UIColor whiteColor];
    }
    // 创建观察者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"changeColor" object:nil];
    

    self.tagsCollection.delegate = self;
    self.tagsCollection.dataSource = self;
    self.tagsCollection.showsHorizontalScrollIndicator = NO;
    [self.tagsCollection registerClass:[WJTagsCollectionCell class] forCellWithReuseIdentifier:TagCell];
    
    [flowLayout release];
    [_tagsCollection release];
    
    
}

- (void)changeColor:(NSNotification *)cation
{
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Mode"] isEqualToString:@"night"]) {
        self.tagsCollection.backgroundColor = FORBLACKCOLOR;
    }else{
        self.tagsCollection.backgroundColor = [UIColor whiteColor];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WJTagsModel *model = self.array[indexPath.row];
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    CGRect rect = [model.name boundingRectWithSize:CGSizeMake(1000, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return CGSizeMake(rect.size.width + 5, 15);
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WJTagsCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:TagCell forIndexPath:indexPath];
    WJTagsModel *model = self.array[indexPath.row];
    cell.lable.text = model.name;
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"点击了Collection: %ld",indexPath.row);
    
     WJTagsModel *model = self.array[indexPath.row];
    self.tagBlock(model.id,model.name);
    
//    NSString *str = [NSString stringWithFormat:@"%ld",model.id];
//    NSString *name = model.name;
//    NSArray *array = @[str,name];
//    /// 发送通知
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"CollectionJump" object:array];
}









//#warning  - 解决重用问题的关键之关键
- (void)setArray:(NSArray *)array
{
    if (_array != array) {
        [_array release];
        _array = [array retain];
    }    
    [self.tagsCollection reloadData];
}











@end
